package com.example.common.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.example.common.utils.ClickUtils;
import com.example.common.utils.L;
import com.example.common.interfaces.LifeCycleListener;

public abstract class AbsViewHolder implements LifeCycleListener {

    private String mTag;
    protected Context mContext;
    protected ViewGroup mParentView;
    protected View mContentView;


    public AbsViewHolder(Context context, ViewGroup parentView) {
        L.e("log----->", "当前ViewHolder路径：" + getClass().getName());
        mTag = getClass().getSimpleName();
        mContext = context;
        mParentView = parentView;
        mContentView = LayoutInflater.from(context).inflate(getLayoutId(), mParentView, false);
        init();
    }

    public AbsViewHolder(Context context, ViewGroup parentView, Object... args) {
        L.e("log----->", "当前ViewHolder路径：" + getClass().getName());
        mTag = getClass().getSimpleName();
        processArguments(args);
        mContext = context;
        mParentView = parentView;
        mContentView = LayoutInflater.from(context).inflate(getLayoutId(), mParentView, false);
        init();
    }

    protected abstract int getLayoutId();

    protected abstract void init();

    protected abstract void processArguments(Object... args);

    protected <T extends View> T findViewById(int viewId) {
        return mContentView.findViewById(viewId);
    }

    public View getContentView() {
        return mContentView;
    }

    protected boolean canClick() {
        return ClickUtils.canClick();
    }

    public void addParent() {
        if (mParentView != null && mContentView != null) {
            mParentView.addView(mContentView);
        }
    }

    public void removeFromParent() {
        ViewParent parent = mContentView.getParent();
        if (parent != null) {
            ((ViewGroup) parent).removeView(mContentView);
        }
    }

    public void subscribeActivityLifeCycle() {
        if (mContext instanceof AbsActivity) {
            ((AbsActivity) mContext).addLifeCycleListener(this);
        }
    }

    public void unSubscribeActivityLifeCycke() {
        if (mContext instanceof AbsActivity) {
            ((AbsActivity) mContext).removeLifeCycleListener(this);
        }
    }

    public void finishActivity() {
        if (mContext != null && mContext instanceof AbsActivity) {
            ((AbsActivity) mContext).finish();
        }
    }

    public void release() {
        L.e("log----->", "release");
    }

    @Override
    public void onCreate() {
        L.e("log----->", "lifeCycle--onCreate");
    }

    @Override
    public void onStart() {
        L.e("log----->", "lifeCycle--onCreate");
    }

    @Override
    public void onReStart() {
        L.e("log----->", "lifeCycle--onCreate");
    }

    @Override
    public void onResume() {
        L.e("log----->", "lifeCycle--onCreate");
    }

    @Override
    public void onPause() {
        L.e("log----->", "lifeCycle--onCreate");
    }

    @Override
    public void onStop() {
        L.e("log----->", "lifeCycle--onCreate");
    }

    @Override
    public void onDestroy() {
        L.e("log----->", "lifeCycle--onCreate");
    }

}
